/**
 * Cesium imagery、 primitive、terrier处理
 */

/**
 * 图层服务类型
 */
const LayerType = {
  IMG: "img",
  WMTS: "wmts",
  WMS: "wms",
  XYZ: "xyz",
  MAPSERVER: "mapserver",
  IMAGESERVER: "imageserver",
  VTILE: "vtile",
  D3TILES: "3dtiles",
  DEBUG: "debug",
  GEOJOSN: "geojson"
};

const init = () => {};

const addLayer = (options, viewer = window.viewer) => {
  let type = options.type;

  let lyr = null;
  switch (type) {
    case LayerType.IMG:
      lyr = createImgLayer(options, viewer);
      break;
    case LayerType.WMTS:
      lyr = createWMTSLayer(options, viewer);
      break;
    case LayerType.WMS:
      lyr = createWmsLayer(options, viewer);
      break;
    case LayerType.XYZ:
      lyr = createXYZLayer(options, viewer);
      break;
    case LayerType.MAPSERVER:
    case "MAPSERVER":
      lyr = createArcgisMapLayer(options, viewer);
      break;
    case LayerType.IMAGESERVER:
    case "IMAGESERVER":
      lyr = createArcgisImageLayer(options, viewer);
      break;
    case LayerType.VTILE:
    case "VTILE":
      lyr = createVTileLayer(options, viewer);
      break;
    case LayerType.D3TILES:
      add3DTiles(options.url, viewer);
      break;
    case LayerType.DEBUG:
      lyr = createDebugerLayer(options, viewer);
  }

  return lyr;
};

/**
 * WMTS
 * @param url
 * @param layerName
 * @param params
 * @param viewer
 * @returns {*}
 */
const createWMTSLayer = param => {
  let tilingScheme = new Cesium.GeographicTilingScheme({
    numberOfLevelZeroTilesX: param.ZeroTilesX,
    numberOfLevelZeroTilesY: param.ZeroTilesY
  });
  if (
    param.srid &&
    (param.srid.indexOf("3857") != -1 ||
      param.srid.indexOf("3785") != -1 ||
      param.srid.indexOf("900913") != -1)
  ) {
    tilingScheme = new Cesium.WebMercatorTilingScheme({
      numberOfLevelZeroTilesX: param.ZeroTilesX,
      numberOfLevelZeroTilesY: param.ZeroTilesY
    });
  }

  let defParam = {
    tilingScheme: tilingScheme
  };

  let lyr = new Cesium.WebMapTileServiceImageryProvider({
    ...defParam,
    ...param
  });
  return lyr;
};

/**
 * arcgis MapServer图层
 * @param url
 * @param viewer
 * @param index   layerindex
 * @returns {ImageryLayer}
 */
const createArcgisMapLayer = param => {
  let defParam = {
    enablePickFeatures: true,
    usePreCachedTilesIfAvailable: true
  };
  let lyr = new Cesium.ArcGisMapServerImageryProvider2({
    ...defParam,
    ...param
  });
  return lyr;
};

/**
 * arcgis ImageServer图层
 * @param url
 * @param viewer
 * @returns {ImageryLayer}
 */
const createArcgisImageLayer = param => {
  let defParam = {
    enablePickFeatures: true,
    usePreCachedTilesIfAvailable: true
  };
  let lyr = new Cesium.ArcGisImageServerImageryProvider({
    ...defParam,
    ...param
  });
  return lyr;
};

/**
 * 矢量瓦片图层
 * @param url
 * @param viewer
 * @returns {ImageryLayer}
 */
const createVTileLayer = param => {
  let defParam = {
    tilingScheme: new Cesium.GeographicTilingScheme(),
    tileWidth: 512,
    tileHeight: 512,
    customTags: {
      l: function(imageryProvider, x, y, level) {
        return level + 2;
      }
    }
  };
  let lyr = new Cesium.UrlTemplateImageryProvider({ ...defParam, ...param });
  return lyr;
};

/**
 * wms图层
 * @param url
 * @param layerName
 * @param params
 * @param viewer
 * @returns {ImageryLayer}
 */
const createWmsLayer = param => {
  let lyr = new Cesium.WebMapServiceImageryProvider(param);
  return lyr;
};

/**
 * xyz图层
 * @param url
 * @param viewer
 * @returns {ImageryLayer}
 */
const createXYZLayer = param => {
  let lyr = new Cesium.UrlTemplateImageryProvider(param);
  return lyr;
};

/**
 * 静态图片图层
 * @param url
 * @param viewer
 */
const createImgLayer = param => {
  let lyr = new Cesium.SingleTileImageryProvider(param);
  return lyr;
};

/**
 * 3DTiles
 * @param url
 * @param viewer
 */
const add3DTiles = (url, viewer) => {
  let tileset = new Cesium.Cesium3DTileset({
    url: url
  });
  tileset.readyPromise
    .then(tileset => {
      viewer.scene.primitives.add(tileset);
      viewer.zoomTo(
        tileset,
        new Cesium.HeadingPitchRange(
          0.0,
          -0.5,
          tileset.boundingSphere.radius * 2.0
        )
      );

      //调整高度
      // let translation = Cesium.Cartesian3.fromArray([0, 0, 0]);
      // let m = Cesium.Matrix4.fromTranslation(translation); //fromTranslation()方法
      // tileset.modelMatrix = m;

      let height = 0;
      let cartographic = Cesium.Cartographic.fromCartesian(
        tileset.boundingSphere.center
      );
      let surface = Cesium.Cartesian3.fromRadians(
        cartographic.longitude,
        cartographic.latitude,
        cartographic.height
      );
      let offset = Cesium.Cartesian3.fromRadians(
        cartographic.longitude,
        cartographic.latitude,
        height
      );
      let translation = Cesium.Cartesian3.subtract(
        offset,
        surface,
        new Cesium.Cartesian3()
      );
      tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
    })
    .otherwise(function(error) {
      console.log(error);
    });

  return tileset;
};

const createDebugerLayer = param => {
  return new Cesium.TileCoordinatesImageryProvider(param);
};

const removeLayer = () => {};

const addTerrain = url => {
  if (url) {
    return new Cesium.CesiumTerrainProvider({
      url
    });
  } else {
    return new Cesium.EllipsoidTerrainProvider();
  }
};

const addDataSource = () => {};

const getPrimitive = () => {};

const removePrimitive = () => {
  getPrimitive();
};

export default {
  LayerType,
  init,
  addLayer,
  removeLayer,
  addTerrain,
  addDataSource,
  removePrimitive
};
